#!/bin/bash

###################################
## Check OCaml Formatting

make check-format
if [ "$?" != 0 ]; then
  echo '** REFUSING TO COMMIT: "$ make check-format" failed'
  exit 1
fi

###################################
## Rebuild Staged Doc Diagrams

check_staged_file() {
  dir="$(dirname "$1")"
  ext="${1##*.}"
  if [ "$dir" = 'docs/res' ] && ([ "$ext" = 'dot' ] || [ "$ext" = 'tex' ]); then
    make "$1.png"
    git add "$1.png"
  fi
}

for stat in $(git status --porcelain -uno); do
  case $stat in
    M*|A*) check_staged_file "$(echo "$stat" | cut -f2-)"; break;;
    R*|C*) check_staged_file "$(echo "$stat" | cut -d'>' -f2)"; break;;
    *) break;;
  esac
done
